package com.example.homework.homework03;

import java.util.List;

// 定义线程任务类，实现Runnable接口
public class CJTask implements Runnable {
    private List<String> prizes;

    public CJTask(List<String> prizes) {
        this.prizes = prizes;
    }


    @Override
    public void run() {
        // 获取线程的名称
        String name = Thread.currentThread().getName();
        while (true) {
            synchronized (this) {
                // 判断奖品是否还有如果没有直接结束循环
                if (prizes.size() <= 0) {
                    // 说明奖品没有了
                    break;
                }
                // 去抽取奖品
                int index = (int) (Math.random() * prizes.size());
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(name + " 抽出了一个： " + prizes.remove(index));
            }
        }
    }
}
